// Legacy Code

// BookSearch
var enter = function(stuff) {
document.search.text.name = stuff
document.search.submit()
};

/*---------------- New Code ----------------*/

/**
 * University Bookstore Website
 * Core Javascript Utilities
 * @author Ian Evans (unless otherwise noted)
 * @copyright University Bookstore 2013
 * @version 0.1.1
 * @requires jquery-1.9.1
 */

/**
 * URL Parameter retriever
 * @summary retrieves URL query string, returns a hashmap containing parameters and their values.
 * @description found on @link http://www.jacklmoore.com/colorbox/faq/#faq-querystring
 */

var getUrlQuery = function(returnArray){
	if (returnArray == null){
		returnArray = false;
	}
    var
    settingsObject = {},
    hash,
    hashes =  location.search.substring(1).split(/\&/),
    i;
    if (returnArray){
    	return hashes;
    }
    for (i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        settingsObject[hash[0]] = hash[1];
    }
    return settingsObject;
}

if($){
	
	/**
	 * Script Includer
	 * @summary A macro function for including other javascript files
	 * @param {string} name Filename or alias for script to load
	 * @param {function} func Function to run after the script loads
	 */
	var loadscript = function(name, func){
		if (typeof func != 'undefined' 
			&& typeof func != 'function') {
			console.log('loadscript(): function supplied is not a function');
		} else {
			if (typeof name != 'undefined'
				&& typeof name != 'string') {
				console.log('loadscript(): name supplied is not a string');
			} else {
			
				/* -------- jquery.cycle -------- */
				if (name === 'cycle'
					|| name === 'jquery.cycle'
					|| name === 'jquery.cycle.js') {
						$.getScript('scripts/jquery/cycle/jquery.cycle.js',function(){
							$.getScript('scripts/jquery/cycle/jquery.cycle.defaults.js',func);
						}).fail(function(){
							console.log("Couldn't load jquery.cycle")
						});
				}
				
				/* -------- jquery.colorbox -------- */
				else if (name === 'colorbox'
						 || name === 'jquery.colorbox'
						 || name === 'jquery.colorbox.js') {
							var cbcss = $('<link>').appendTo('head');
							cbcss.attr({
								rel: 'stylesheet',
								type: 'text/css',
								href: 'css/colorbox.css'
							});
							$.getScript('scripts/jquery/colorbox/jquery.colorbox.js',function(){
								$.getScript('scripts/jquery/colorbox/jquery.colorbox.defaults.js',func);
							}).fail(function(){
								console.log("Couldn't load jquery.colorbox");
							});
				}
				
				/* -------- colorbox quickview -------- */
				else if (name === 'quickview'
						 || name === 'jquery.colorbox.quickview') {
							$.getScript('scripts/jquery/colorbox/jquery.colorbox.quickview.js',func);
							var qvcss = $('<link>').appendTo('head');
							qvcss.attr({
								rel: 'stylesheet',
								type: 'text/css',
								href: 'css/quickview.css'
							});
				}
				
				/* -------- cycle headline -------- */
				else if (name === 'headline'
						 || name === 'jquery.cycle.headline') {
							$.getScript('scripts/jquery/cycle/jquery.cycle.headline.js',func);
				}
				
				/* -------- cycle banner -------- */
				else if (name === 'banner' 
						 || name === 'jquery.cycle.banner') {
							$.getScript('scripts/jquery/cycle/jquery.cycle.banner.js',func);
				}
				
				/* -------- other scripts -------- */
				// takes literal file name/path
				else {
					$.getScript('scripts/' + name,func).fail(function(){
						console.log('Error: ' + name + ' not found in scripts directory');
					});
				};
			};
		};

	};
	
	/** 
	 * Open Link in New Tab
	 * @summary Will make all elements specified by selector open their href url in a new tab on click if tabbed browsing is available.
	 * @requires jquery-1.9.1
	 * @param {string} selector A jQuery Selector String
	 */
	var newtab = function(selector){
		$(selector).each(function(){
			if ($(this).prop('href')){
				$(this).bind('click',function(event){
					event.preventDefault();
					window.open($(this).prop('href'));
				});
			} else {
				console.log(selector + ': not a link or does not have href property defined.');
			};
		});
	};
	
	/** 
	 * Open Link in Popup
	 * @summary Will make all elements specified by selector open their href url in a new window of a particular size.
	 * @deprecated Please use newtab() or colorbox.
	 * @requires jquery-1.9.1
	 * @param {string} selector A jQuery Selector String
	 */
	var popup = function(selector){
		$(selector).each(function(){
			if($(this).prop('href')){
				console.log('found ' + selector + ' ' + $(this).prop('href'));
				$(this).bind('click',function(event){
					event.preventDefault();
					var popup = window.open($(this).prop('href'),'comnt_win','status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=800,height=300,left=10,top=10');
					popup.focus();
				});
			} else {
				console.log(selector + ': not a link or does not have href property defined.');
			};
		});
	};
};